import axios from "axios"
import { Notification } from 'element-ui'
import router from '@/router'

axios.defaults.baseURL = 'http://s.xsand.cn';
// axios.defaults.baseURL = 'http://39.104.185.119:3000';


// axios.defaults.headers['Content-Type'] = 'application/x-www-form-urlencoded';
axios.defaults.headers.post['Content-Type'] = 'application/json';

// 添加请求拦截器
axios.interceptors.request.use(function (config) {
    console.log(router)
    let uuid = window.sessionStorage.getItem('uuid')
    if (uuid == null || !uuid) {
        router.push({
            path: '/login'
        })
    }
    // 在发送请求之前做些什么
    return config;
}, function (error) {
    // 对请求错误做些什么
    return Promise.reject(error);
});

// 添加响应拦截器
axios.interceptors.response.use(function (response) {
    console.log(response)
    // 对响应数据做点什么
    if (response.status == 200) {
        if (response.data.code == -1) {
            Notification.error(response.data.msg);
            return false
        } else {
            return response.data
        }
    }
    return response;
}, function (error) {
    Notification.error('网络异常');
    // 对响应错误做点什么
    return Promise.reject(error);
});


export default axios